package com.wt.controller;

import com.alibaba.fastjson.JSONObject;
import com.wt.entity.Order;
import com.wt.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/SelectOrderServlet")
public class SelectOrderServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置编码
        response.setContentType("application/json;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        String userId = request.getParameter("userId");
        //获取并验证参数
        if (userId == null){
            PrintWriter out = response.getWriter();
            String err = "{\"error\":\"errorParameter\"}";
            out.write(err);
            return;
        }
        OrderService orderService = new OrderService();
        try {
            List<Order> orders = orderService.selectAllOrder(userId);
            if (orders == null){
                PrintWriter out = response.getWriter();
                String err = "{\"error\":\"null\"}";
                out.write(err);
                return;
            }
            PrintWriter out = response.getWriter();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("data",orders);
            out.write(jsonObject.toString());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
